package com.ydx.demo.controller;

import com.ydx.demo.CommonEntity.ResponseData;
import com.ydx.demo.entity.Medicine;
import com.ydx.demo.entity.Shopcart;
import com.ydx.demo.mapper.MedicineMapper;
import com.ydx.demo.service.MedicineService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.origin.Origin;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@CrossOrigin(origins = "*") //   允许所有域名访问
public class MedicineController {
    @Resource
    MedicineMapper medicineMapper;

    //查询所有药品
    @GetMapping("/me")
    public ResponseData getMedicine(){
        List<Medicine> data = medicineMapper.finAll();
        return new ResponseData("200",null,true, data);
    }

    //模糊查询药品
    @PostMapping("/findme")
    public ResponseData findMedicine(@RequestBody Medicine medicine){
        List<Medicine> data =  medicineMapper.seleceMedicine(medicine);
        return new ResponseData("200",null,true, data);
    }

    //添加药品
    @PostMapping("/me")
    public String setMedicine(@RequestBody Medicine medicine){//接收从前端传来的数据
        medicineMapper.add(medicine);
        return "success";
    }

    //修改药品
    @PostMapping("/updateMe")
    public String updateMedicine(@RequestBody Medicine medicine){
        medicineMapper.UpdateMedicine(medicine);
        return "修改成功";
    }

    //修改药品表的图片地址
    @PostMapping("/updateImage")
    public ResponseData updateImage(@RequestBody Medicine medicine){
        medicineMapper.UpdateImages(medicine);
        return new ResponseData("200",null,true, "修改成功");
    }

    //删除药品
    @PostMapping("/delMe")
    public ResponseData deleteMedicine(@RequestBody Medicine medicine){
        int data = medicineMapper.DelMedicine(medicine);
        return new ResponseData("200" ,null,true, "删除成功");
    }

    //购物车  查找购物车所有
    @GetMapping("/shop")
    public List<Shopcart> getShopcart(){
        return medicineMapper.selectShopAll();
    }

    // 加入购物车
    @PostMapping("/insertShopping")
    public ResponseData insetShoppingtrolley(@RequestBody Shopcart shopcart){
        medicineMapper.insertShoppingtrolley(shopcart);
        return new ResponseData("200",null,true,"新增成功");
    }

    //左连接多表联查
    @GetMapping("/selshop")
    public List<Shopcart> getShop(){
        return medicineMapper.selectShop();
    }

    //删除购物车数据
    @PostMapping("/delShopping")
    public ResponseData deleteMedicine(@RequestBody Shopcart shopcart){
        int data = medicineMapper.DelShopping(shopcart);
        return new ResponseData("200" ,null,true, "删除成功");
    }











    //----------------------------------------------
    @Autowired
    private MedicineService medicineService;
    @GetMapping("/mes")
    public ModelAndView findAllMedicines(){
        List<Medicine> mes = medicineService.findAllMedicines();
        ModelAndView mv = new ModelAndView();
        mv.addObject("mes",mes);
        mv.setViewName("mes");
        return mv;
    }
    @GetMapping("/mes/{id}")
    public ModelAndView findMedicineById(@PathVariable("id") int id){
        Medicine medicine = medicineService.findMedicineById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("medicine",medicine);
        mv.setViewName("medicine");
        return mv;
    }
    @PostMapping("/addMedicine")
    public  ModelAndView addMedicine(Medicine medicine){
        medicineService.addMedicine(medicine);
        ModelAndView mv = new ModelAndView();
        //添加成功后，跳转到查找所有学生的控制器
        mv.setViewName("redirect:/mes");
        return  mv;
    }
    @GetMapping("/addMedicine")
    public String addMedicine(){
        return "addMedicine";
    }
//    @GetMapping("/delectMedicine/{id}")
//    public ModelAndView deleteMedicine(@PathVariable("id") int id){
//        medicineService.deleteMedicine(id);
//        ModelAndView mv = new ModelAndView();
//        //删除成功后，跳转到查找所有学生的控制器
//        mv.setViewName("redirect:/mes");
//        return mv;
//    }
    @GetMapping("/updateMedicine/{id}")
    public ModelAndView toUpdateMedicine(@PathVariable("id")int id){
        Medicine medicine = medicineService.findMedicineById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("medicine",medicine);
        mv.setViewName("updateMedicine");
        return mv;
    }
//    @PostMapping("/updateMedicine")
//    public ModelAndView updateMedicine(Medicine medicine){
//        medicineService.updateMedicine(medicine);
//        System.out.println(medicine);
//        ModelAndView mv = new ModelAndView();
//        //修改成功后，跳转到查找所有学生的控制器
//        mv.setViewName("redurect:/mes");
//        return mv;
//    }

}
